package thinkinginjava.concurrency;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Memoizer2<K, V> implements Computable<K, V>{
	private final Map<K, V> cache = new ConcurrentHashMap<K, V>();
	private final Computable<K, V> c;
	
	public Memoizer2(Computable<K, V> c) {
		super();
		this.c = c;
	}


	@Override
	public V compute(K arg) throws InterruptedException {
		V re = cache.get(arg);
		if(re == null) {
			re = c.compute(arg);
			cache.put(arg, re);
		}
		return re;
	}


}
